<?php 
if(KT!='KhiThe') exit;

class PositionModules_model extends khithe_mysql{

    public function get_pos_mod($position, $module, $com,$rs=false)
    {
        $query = "SELECT ID FROM ".modules_position."
        WHERE Position = '".$position."' AND ModuleID = '".$module."' AND ComID = '".$com."'";
        $row = parent::mysql_fetch_one_rows($query);
        if($rs==true) return $row['ID'];
        $rc = empty($row['ID'])?0:1;
        return $rc;
    }

    public function set_pos_mod($position, $module, $com, $order)
	{
        $num = $this->get_pos_mod($position, $module, $com);
        if($num==0){
            $query = "INSERT INTO ".modules_position."(Position,ModuleID,ComID,`Order`,`Status`)
					  VALUES('".$position."','".$module."','".$com."','".$order."','true')";
        }else{
            $query = "UPDATE ".modules_position." SET `Order` = '".$order."'
            WHERE Position = '".$position."' AND ModuleID = '".$module."' AND ComID = '".$com."'";
        }
        parent::UpdateInsertMySQL($query);
	}

    public function del_pos_mod($position,$com_id,$str)
    {
        $del = "DELETE FROM ". modules_position ."
        WHERE Position = '".$position."' AND ComID = '".$com_id."' AND
        ID NOT IN ($str) ";
        $del_qr = parent::DeleteMySQL($del);
        parent::CloseConnection();
        return $del_qr;
    }

	public function get_active_modules($str)
	{
		$select = "SELECT * FROM ".modules." WHERE ID NOT IN ($str) ORDER BY ID DESC";
		$result = parent::SelectMySQL($select);
		parent::CloseConnection();
		return $result;
	}

	public function getmodules()
	{
		$select = "SELECT * FROM ".modules." ORDER BY ID DESC";
		$result = parent::SelectMySQL($select);
		parent::CloseConnection();
		return $result;
	}

	public function getPositionModuleID($ID){
		
		$ID = parent::checkStrMySQL($ID);
		$select = "SELECT * FROM ".modules_position." WHERE ID = '".$ID."'";
		$result= parent::SelectMySQL($select);
		parent::CloseConnection();
		return $result;
	}
	
	public function setCheckedPositionModules($ID,$Val)
	{
		$ID = parent::checkStrMySQL($ID);
		$update = "UPDATE ".modules_position." SET Status='".$Val."'  WHERE ID = '".$ID."'";
		$update_qr = parent::UpdateInsertMySQL($update);
		parent::CloseConnection();
		return $update_qr;
	}
	
	public function delCheckedPositionModules($ID)
	{
		$ID = parent::checkStrMySQL($ID);
		$del = "DELETE FROM ".modules_position." WHERE ID='".$ID."' ";
		$del_qr = parent::DeleteMySQL($del);
		parent::CloseConnection();
		return $del_qr;
	}
	
	public function getPositionModules($ComID=1)
	{		
		$select = " SELECT 
						".modules.".ID as MID,
						".modules_position.".ID,
						".modules_position.".Position,
						".modules_position.".Order,
						".modules_position.".`Status`,
						".components.".ComTitle,
						".modules.".ModuleTitle,
						".modules.".ModuleDir
					FROM ".modules_position.",".modules.",".components." 
					WHERE ".modules_position.".ModuleID = ".modules.".ID 
					AND ".modules_position.".ComID = ".components.".ID 
					AND ".components.".ID ='$ComID'
					ORDER BY Position ASC, `Order` ASC
		";
		$result = parent::SelectMySQL($select);
		parent::CloseConnection();
		return $result; 
	}
	
	public function getComponents($ID=NULL)
	{	$str = '';	
		if($ID!=NULL) $str = " AND ID=$ID ";
		$select = " SELECT * FROM ".components." WHERE ".components.".Status='true' $str ";
		$result = parent::SelectMySQL($select);
		parent::CloseConnection();
		return $result; 
	}
	
}
?>